<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img
          src="../assets/image/heima.png"
          alt
        >
        <span>后台管理</span>
      </div>
      <el-button
        type="info"
        @click="logout"
      >
        退出
      </el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏区域 -->
      <transition name="el-fade-in-linear">
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div
            class="togger-button"
            @click="toggeCollapse"
          >
            |||
          </div>
          <el-menu
            background-color="deepskyblue"
            text-color="#fff"
            active-text-color="#ffd04b"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            :default-active="isActive"
          >
            <!-- 一级菜单 -->
            <el-submenu
              v-for="item in menulist"
              :key="item.id"
              :index="item.id+''"
            >
              <!-- 一级菜单区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconsObj[item.id]" />
                <!-- 文本 -->
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                v-for="subItem in item.children"
                :key="subItem.id"
                :index="'/'+subItem.path"
                @click="saveNavState"
              >
                <!-- 二级菜单区域 -->
                <template slot="title">
                  <!-- 图标 -->
                  <i class="el-icon-menu" />
                  <!-- 文本 -->
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
      </transition>
      <!-- 内容主体 -->

      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      isActive: ''
    }
  },
  created () {
    this.getMenuList()
    this.saveNavState()
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    toggeCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState () {
      const activePath = this.$route.path
      console.log(activePath)
      window.sessionStorage.setItem('activePath', activePath)
      this.isActive = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: lightskyblue;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: deepskyblue;
  .el-menu {
    border-right: none;
  }
}
.el-container {
  background-color: mintcream;
}
.el-main {
  background-color: mintcream;
}
.togger-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
